<?php

class Search_model extends CI_Model {

	// Get total posts by search query
	function get_posts($query, $post_per_page,$current_page) {
		$query = trim($query);
		$query = str_ireplace(' ','%',$query);
		$query = '%'.$query.'%';
		if (strlen($query) > 6) {
			$sql = "SELECT *,job.description AS description,job.location AS location
					FROM job
					INNER JOIN company
					ON company.company_id = job.company_id
					WHERE MATCH (job.title, job.description, job.location) AGAINST (?)";
					//AND NOW() < due_date";
					$query = $this->db->query($sql, array($query));
		} else {
			$sql = "SELECT *,job.description AS description,job.location AS location
					FROM job
					INNER JOIN company
					ON company.company_id = job.company_id
					WHERE job.title LIKE ? OR job.description LIKE ? OR job.location LIKE ?";
					//AND NOW() < due_date";
					$query = $this->db->query($sql, array($query,$query,$query));
		}
		
		return $query->result_array();
	}
 	
 	
 	function highlightWords($string,$words,$ajax=false){
 		$words=explode(' ',$words);
 		for($i=0;$i<sizeOf($words);$i++) {
 			if($ajax==true)
 			{
 				$string=str_ireplace($words[$i], '<strong class=\"highlight\">'.$words[$i].'<\/strong>', $string);
 			} else {
 				$string=str_ireplace($words[$i], '<strong class="highlight">'.$words[$i].'</strong>', $string);
 			}
  		}
 		return $string;
	}
	
	function cleanHTML($input, $ending='...') {
 
    	$output = strip_tags($input);
 
    	$output = substr($output, 0, 100);
    	$output .= $ending;
    
    
    	return $output;
	}

	
}

?>